<template>
	<div>
		<div>
			<specify-card
				:pageTitle="'登录'"
				:cardImg="'kurisu.png'"
				@login="userLogin"
			></specify-card>
		</div>
		<Transition name="codeImg">
			<imgVerifyCode
				@verify="verification"
				v-if="verifyShow"
			></imgVerifyCode>
		</Transition>
	</div>
</template>

<script setup>
import { user } from '../store/user.js';
import { date } from '../store/date.js';
import { useRouter } from 'vue-router';
import { onMounted, ref } from 'vue';
import axios from 'axios';
const router = useRouter();
const userStore = user();
const dateStore = date();

let userType = ref('');
let verifyShow = ref(false);
let verifyState = ref(true);
let verifyCount = ref(0);

const verification = async (code) => {
	const token = sessionStorage.getItem('code');
	try {
		const res = await axios({
			baseURL: 'http://localhost:5000',
			url: '/verifyCode',
			method: 'post',
			data: {
				token,
				code,
			},
		});
		if (res.data.state === 'ok') {
			verifyState.value = true;
			verifyCount.value = 0;
			verifyShow.value = false;
			return ElNotification({
				title: 'Success',
				message: '验证成功',
				type: 'success',
			});
		} else {
			return ElNotification({
				title: 'Warning',
				message: '验证码错误，请重新输入一次TnT',
				type: 'warning',
			});
		}
	} catch (error) {
		console.log(error);
		return ElNotification({
			title: 'Error',
			message: '服务器错误',
			type: 'error',
		});
	}
};

const userLogin = async (username, password) => {
	if (username && password) {
		if (!verifyState.value) {
			return (verifyShow.value = true);
		}
		try {
			userType = await userStore.validate(username, password);
			console.log(userType);
			if (userType === 'error' || userType === 'none') {
				verifyCount.value += 1;
				if (verifyCount.value >= 1) {
					verifyState.value = false;
				}
				ElNotification({
					title: 'Error',
					message: '帐号名或密码输入错误',
					type: 'error',
				});
				setTimeout(() => {
					return (verifyShow.value = true);
				}, 500);
			} else {
				localStorage.setItem('usertype', userType);
				const userData = await userStore.getSpecifyUserMsg(
					username,
					password
				);
				let date = dateStore.getNowDate();
				localStorage.setItem('date', date);
				localStorage.setItem('id', userData.id);
				localStorage.setItem('username', userData.username);
				localStorage.setItem('nickname', userData.nickname);
				localStorage.setItem('avatar', userData.avarar);
				localStorage.setItem('notes', userData.notes);
				ElNotification({
					title: 'Success',
					message: '登录成功',
					type: 'success',
				});
				return router.push('/');
			}
		} catch (error) {
			console.log(error);
			return ElNotification({
				title: 'Error',
				message: '服务器错误',
				type: 'error',
			});
		}
	} else {
		return ElNotification({
			title: 'Warning',
			message: '您还没有输入用户名或密码',
			type: 'warning',
		});
	}
};
</script>

<style scoped>
.codeImg-enter-active,
.codeImg-leave-active {
	transition: opacity 1s ease;
}
.codeImg-enter-from,
.codeImg-leave-to {
	opacity: 0;
}
</style>
